package cn.bgs.controller;

import cn.bgs.pojo.*;
import cn.bgs.service.StudentInformationService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/*
@Auther fyj
@Date 2021/7/15
@Time 20:19
*/
@Controller
public class StudentInformationController {
    Logger log= LogManager.getLogger();
    @Autowired
    private StudentInformationService studentInformationService;

    @ResponseBody
    @RequestMapping("/findStudentInformation")
    public Map<String,Object> findStudentInformation(@RequestParam(required=false,defaultValue="1") int page,
                                                     @RequestParam(required=false,defaultValue="7") int limit,
                                            String stuName, String mobile, String sex,Integer age,String idNum,String gradeName ){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<StudentInformation> stu  = studentInformationService.findStudentInformation(stuName,mobile,sex,age,idNum,gradeName);// 这是我们的sql
        for(StudentInformation i:stu){
            i.setNativePlace(i.getNativePlace());
        }

        log.info("data_______:::::"+stu);
        log.info("String stuName, String mobile, String sex,Integer age,String idNum：：：：：：："
                +stuName+"   "
                +mobile+"   "
                +sex+"   "
                +age+"   "
                +idNum+"   "
        );
        // 使用pageInfo包装查询
        PageInfo<StudentInformation> rolePageInfo = new PageInfo<>(stu);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }
    @ResponseBody
    @RequestMapping("/findDormitoryStu")
    public Map<String,Object> findDormitoryStu(@RequestParam(required=false,defaultValue="1") int page,
                                                     @RequestParam(required=false,defaultValue="7") int limit,
                                                     String dormitoryName){

        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<StudentInformation> stu  = studentInformationService.findDormitoryStu(dormitoryName);// 这是我们的sql
        for(StudentInformation i:stu){
            i.setNativePlace(i.getNativePlace());
        }


        // 使用pageInfo包装查询
        PageInfo<StudentInformation> rolePageInfo = new PageInfo<>(stu);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }
    @ResponseBody
    @RequestMapping("/findGradeStu")
    public Map<String,Object> findGradeStu(@RequestParam(required=false,defaultValue="1") int page,
                                           @RequestParam(required=false,defaultValue="7") int limit,
                                          String gradeName){

        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<StudentInformation> stu  = studentInformationService.findGradeStu(gradeName);// 这是我们的sql
        for(StudentInformation i:stu){
            i.setNativePlace(i.getNativePlace());
        }

        log.info("data_______:::::"+stu);
        log.info("gradeName：：：：：：："
                +gradeName+"   "
        );
        // 使用pageInfo包装查询
        PageInfo<StudentInformation> rolePageInfo = new PageInfo<>(stu);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }
    @ResponseBody
    @RequestMapping("/studentDelete")
    public Integer studentDelete(@RequestBody String data){
        JSONObject jsonObject= JSONObject.parseObject(data);
        Integer stuId=Integer.parseInt(jsonObject.getString("stuId"));
        return studentInformationService.studentDelete(stuId);
    }

    @ResponseBody
    @RequestMapping("/updateView")
    public StudentInformation updateView(@RequestBody String stu){
        JSONObject jsonObject=JSONObject.parseObject(stu);
        Integer stuId=Integer.parseInt(jsonObject.getString("stuId"));
        return studentInformationService.updateView(stuId);
    }

    @ResponseBody
    @RequestMapping("/provinces")
    public List<Provinces> provinces(){
        return studentInformationService.provinces();
    }

    @ResponseBody
    @RequestMapping("/cities")
    public List<Cities> cities(){
        return studentInformationService.cities();
    }

    @ResponseBody
    @RequestMapping("/areas")
    public List<Areas> areas(){
        return studentInformationService.areas();
    }

    @ResponseBody
    @RequestMapping("/updateStu")
    public Integer updateStu(@RequestBody String updDate){

        JSONObject jsonObject=JSONObject.parseObject(updDate);
        StudentInformation stu=new StudentInformation();
        stu.setStuId(Integer.parseInt(jsonObject.getString("stuId")));
        stu.setStuName(jsonObject.getString("stuName"));
        stu.setMobile(jsonObject.getString("mobile"));
        stu.setSex(jsonObject.getString("sex"));
        stu.setIdType(jsonObject.getString("idType"));
        stu.setIdNum(jsonObject.getString("idNum"));
        stu.setProvincesId(Integer.parseInt(jsonObject.getString("provincesId")));
        stu.setCitiesId(Integer.parseInt(jsonObject.getString("citiesId")));
        stu.setAreasId(Integer.parseInt(jsonObject.getString("areasId")));
        stu.setDetailAddr(jsonObject.getString("detailAddr"));

        Calendar now = Calendar.getInstance();//获取时间
        stu.setUpdateDate(now.getTime());//当前时间


        return studentInformationService.updateStu(stu);
    }

    @ResponseBody
    @RequestMapping("/insertStu")
    public Integer insertStu(@RequestBody String istDate){

        JSONObject jsonObject=JSONObject.parseObject(istDate);
        StudentInformation stu=new StudentInformation();
        stu.setStuNum(Integer.parseInt(jsonObject.getString("stuNum")));
        stu.setStuName(jsonObject.getString("stuName"));
        stu.setMobile(jsonObject.getString("mobile"));
        stu.setSex(jsonObject.getString("sex"));
        stu.setAge(Integer.parseInt(jsonObject.getString("age")));
        stu.setIdType(jsonObject.getString("idType"));
        stu.setIdNum(jsonObject.getString("idNum"));
        stu.setBirthday(jsonObject.getDate("birthday"));
        stu.setProvincesId(Integer.parseInt(jsonObject.getString("provincesId")));
        stu.setCitiesId(Integer.parseInt(jsonObject.getString("citiesId")));
        stu.setAreasId(Integer.parseInt(jsonObject.getString("areasId")));
        stu.setDetailAddr(jsonObject.getString("detailAddr"));
        stu.setDeleteFlag(0);
        Calendar now = Calendar.getInstance();//获取时间
        stu.setCreateDate(now.getTime());
        stu.setUpdateDate(now.getTime());//当前时间


        return studentInformationService.insertStu(stu);
    }
    @RequestMapping("/deleteList")
    @ResponseBody
    public Integer deleteList(@RequestBody String jsonStr){

        JSONObject jsonObject=JSONObject.parseObject(jsonStr);
        JSONArray ids=jsonObject.getJSONArray("stuIds");
        Integer[] stuIds=new Integer[ids.size()];
        for(int i=0;i<ids.size();i++){
            Object id = ids.get(i);
            stuIds[i]=Integer.parseInt(id.toString());
        }
        List<Integer> list= Arrays.asList(stuIds);

        return studentInformationService.deleteList(list);
    }
    @RequestMapping("/insertAdd")
    @ResponseBody
    public Integer insertAdd(@RequestBody String istData){

        JSONObject jsonObject = JSONObject.parseObject(istData);
        StudentInformation studentInformation = new StudentInformation();
        studentInformation.setStuId(jsonObject.getInteger("stuId"));
        studentInformation.setGradeId(jsonObject.getString("gradeId"));
        studentInformation.setGradeName(jsonObject.getString("gradeName"));
        return studentInformationService.insertAdd(studentInformation);
    }
    @RequestMapping("/insertAddStu")
    @ResponseBody
    public Integer insertAddStu(@RequestBody String istData){

        JSONObject jsonObject = JSONObject.parseObject(istData);
        StudentInformation studentInformation = new StudentInformation();
        studentInformation.setStuId(jsonObject.getInteger("stuId"));
        studentInformation.setDormitoryId(jsonObject.getString("dormitoryId"));
        studentInformation.setDormitoryName(jsonObject.getString("dormitoryName"));
        return studentInformationService.insertAddStu(studentInformation);
    }
    @RequestMapping("/updatestuName")
    @ResponseBody
    public Integer updatestuName(@RequestBody StudentInformation studentInformation ){

        return studentInformationService.updatestuName(studentInformation);

    }
}
